package com.yykj.weifenxiao.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.yykj.common.util.HttpUtil;
import com.yykj.drup.client.Product;
import com.yykj.weifenxiao.server.config.WiFenXiaoConfig;
import com.yykj.weifenxiao.server.mapper.ProductDao;
import com.yykj.weifenxiao.server.model.WIProduct;
import com.yykj.weifenxiao.server.model.WIProductSkus;
import com.yykj.weifenxiao.server.service.AccessTokenService;
import com.yykj.weifenxiao.server.service.CategoryService;
import com.yykj.weifenxiao.server.service.WiFenXiaoProductService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zeng
 * @Date 2022/3/9
 */
@Service
@AllArgsConstructor
public class WiFenXiaoProductServiceImpl extends ServiceImpl<ProductDao, Product> implements WiFenXiaoProductService {

    final AccessTokenService accessTokenService;
    final ProductDao productDao;
    final CategoryService categoryService;
    @Override
    public void insertOrUpdateProduct(List<WIProduct> wiProducts) {
        wiProducts.forEach(wiProduct ->{
            Product product = new Product();
            BeanUtil.copyProperties(wiProduct,product);
            String[] imgs = wiProduct.getImg();
            //设置药品小图
            if (imgs.length != 0){
                product.setProductImg(imgs[0]);
                product.setProductImgDetail(ArrayUtil.join(imgs,","));
            }
            //设置药品详情图
            if (!wiProduct.getSkus().isEmpty()){
                WIProductSkus skus = wiProduct.getSkus().get(0);
                product.setSkuId(skus.getItemId());
                product.setAlias(skus.getAlias());
                product.setSaleNum(skus.getNum());
                product.setSkuName(skus.getSkuName());
            }
            //设置分类信息
            String categoryId = wiProduct.getCategoryId();
            Integer id = categoryService.initCategory(categoryId);
            product.setCategoryId(id);
            //插入商品信息
            QueryWrapper<Product> wrapper = new QueryWrapper<>();
            wrapper.eq("item_id",product.getItemId());
            Product one = productDao.selectOne(wrapper);
            if (one == null){
                productDao.insert(product);
            }else {
                productDao.update(product,wrapper);
            }
        });
    }

    @Override
    public List<WIProduct> productList() {
        String accessToken = accessTokenService.getAccessToken();
        String requestUrl = "/item/list";
        Map<String,Object> map = new HashMap<>();
        map.put("access_token",accessToken);
        try {
            String body = HttpUtil.get(WiFenXiaoConfig.baseUrl + requestUrl, map);
            System.out.println(body);
            JSONObject jsonObject = JSONUtil.parseObj(body);
            Object product = jsonObject.get("itemList");
            JSONArray array = JSONUtil.parseArray(product);
            List<WIProduct> products = JSONUtil.toList(array, WIProduct.class);
            return products;
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return null;
    }
}
